﻿

$(document).ready(function () {
  
});


$(window).resize(function () {
    _closePopup();
});

function _openPopup(tittle) {
    $("body").append("<div id='mask2'></div>");
    $("#mask2").fadeIn(300);
    
    var loginBox = "#popup_box";
    $(loginBox + " .popup_title").html(tittle);

    setTimeout(function () {    
        var win_h = $(this.window).height() - 200;
        var pop_h = $(loginBox).height();
        var popMargTop = (win_h - pop_h) / 2;

        var pop_w = $(loginBox).width();
        var popMargLeft = ($(window).width() - pop_w) / 2;
        var popMargLeft = popMargLeft;

        $(loginBox).css({
            "margin-top": popMargTop
            , "margin-left": popMargLeft
        });

        $(loginBox).fadeIn(400);
    }, 500);  
}

function _closePopup() {
    $("#mask2, .login-popup").fadeOut(100, function () {
        $("#popup_content").empty();
        $("#mask2").remove();
    });
}


function Popup_Event() {
    $("body").append("<div id='mask2'></div>");
    $("#mask2").fadeIn(300);

    var loginBox = "#popup_Event";   

    setTimeout(function () {
        var win_h = $(this.window).height() - 200;
        var pop_h = $(loginBox).height();
        var popMargTop = (win_h - pop_h) / 2;

        var pop_w = $(loginBox).width();
        var popMargLeft = ($(window).width() - pop_w) / 2;
        var popMargLeft = popMargLeft;

        $(loginBox).css({
            "margin-top": popMargTop
            , "margin-left": popMargLeft
        });

        $(loginBox).fadeIn(400);
    }, 500);
}

function Popup_Event_close() {
    $("#mask2, .login-popup").fadeOut(100, function () {
        $("#pop_Event_Content").empty();
        $("#mask2").remove();
    });
}


function showLoading() {

    $("body").append("<div id='bg_loading'></div>");
    $("body").append("<div id='loading'></div>");

    var popMargTop = ($(window).height() - 300) / 2;
    var popMargLeft = ($(window).width() - 400) / 2;

    $("#loading").css({
        "margin-top": -popMargTop
        , "margin-left": popMargLeft
    });

    $("#bg_loading").fadeIn(300);
    $("#loading").fadeIn(300);

}
function hideLoading() {

    $("#bg_loading , #loading").fadeOut(100, function () {
        $("#loading").remove();
        $("#bg_loading").remove();
    });

}


function cusAlert(content) {
    $("#alert_content").text(content);    
    var loginBox = "#alert-box"; 
    $("body").append("<div id='mask'></div>");
    $("#mask").fadeIn(300);
    
    setTimeout(function () {    
    var win_h = $(this.window).height() - 200;
    var pop_h = $(loginBox).height();
    var popMargTop = (win_h - pop_h) / 2;   
        $(loginBox).css({
            "margin-top": popMargTop            
        });
        $(loginBox).fadeIn(400);
       setTimeout(function () { closeAlert(); }, 2000);

    }, 100);  
    return false;
}

function closeAlert() {
    $("#mask , .alert-popup").fadeOut(100, function () {
        $("#alert_content").empty();
        $("#mask").remove();
        hideLoading();
    });
}


// autoload content StoreList
// param url, so page, so item hien thi, tong so page
function loadMore(posUrl, page, itemNum, totalpage) {
    $contentLoadTriggered = false;
    $("#mainDiv").scroll(
         function () {
             if ($("#mainDiv").scrollTop() >= ($("#wrapperDiv").height() - $("#mainDiv").height()) && $contentLoadTriggered == false) // $contentLoadTriggered == false)
             {

                 $contentLoadTriggered = true;
                 if (page < totalpage) {
                     showLoading();
                     page++;
                     $.ajax({ type: 'POST', url: posUrl, data: { 'NumPage': page, 'NumRow': itemNum }, success: function (response) {
                         $("#countPage").val(page);
                         $("#tbLandList").append(response).trigger('create');
                         $contentLoadTriggered = false;
                         hideLoading();
                     }
                     });
                 } else {
                     page = 1;
                     $("#countPage").val(page);
                     $contentLoadTriggered = true;
                 }
             }
         });
}